IntParser.sign_   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2017-2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview Encode and decode int numbers to and from byte buffers.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
/**
31
 * A class to write and read integer numbers to and from byte buffers.
32
 */
33
export class IntParser {
34
  
35
  /**
36
   * @param {number} bits The number of bits used by the integer.
37
   * @param {boolean} [signed=false] True for signed, false otherwise.
38
   * @param {boolean} [clamp=false] True to clamp on overflow.
39
   */
40
  constructor(bits, signed=false, clamp=false) {
41
    /**
42
     * The number of bits used by one number.
43
     * @type {number}
44
     */
45
    this.bits = bits;
46
    /**
47
     * The number of bytes used by one number.
48
     * @type {number}
49
     */
50
    this.offset = Math.ceil(bits / 8);
51
    /**
52
     * @type {number}
53
     * @protected
54
     */
55
    this.max = Math.pow(2, bits) - 1;
56
    /**
57
     * @type {number}
58
     * @protected
59
     */
60
    this.min = 0;
61
    /**
62
     * @type {Function}
63
     */
64
    this.unpack = this.unpack_;
65
    if (signed) {
66
      this.max = Math.pow(2, bits) / 2 - 1;
67
      this.min = -this.max - 1;
68
      this.unpack = this.unpackSigned_;
69
    }
70
    if (clamp) {
71
      this.overflow_ = this.clamp_;
72
    }
73
  }
74
75
  /**
76
   * Write one unsigned integer to a byte buffer.
77
   * @param {!(Uint8Array|Array<number>)} buffer An array of bytes.
78
   * @param {number} num The number. Overflows are truncated.
79
   * @param {number} [index=0] The index being written in the byte buffer.
80
   * @return {number} The next index to write on the byte buffer.
81
   * @throws {RangeError} On overflow if clamp is set to false.
82
   * @throws {TypeError} If num is not a integer.
83
   */
84
  pack(buffer, num, index=0) {
85
    if (typeof num !== "number" || !isFinite(num) || Math.floor(num) !== num) {
86
      throw new TypeError();
87
    }
88
    num = this.overflow_(num);
89
    for (let i = 0, len = this.offset; i < len; i++) {
90
      buffer[index] = Math.floor(num / Math.pow(2, i * 8)) & 255;
91
      index++;
92
    }
93
    return index;
94
  }
95
96
  /**
97
   * Read one unsigned integer from a byte buffer.
98
   * Does not check for overflows.
99
   * @param {!(Uint8Array|Array<number>)} buffer An array of bytes.
100
   * @param {number} [index=0] The index to read.
101
   * @return {number}
102
   * @private
103
   */
104
  unpack_(buffer, index=0) {
105
    /** @type {number} */
106
    let num = 0;
107
    for(let x = 0; x < this.offset; x++) {
108
      num += buffer[index + x] * Math.pow(256, x);
109
    }
110
    return num;
111
  }
112
113
  /**
114
   * Read one two's complement signed integer from a byte buffer.
115
   * @param {!(Uint8Array|Array<number>)} buffer An array of bytes.
116
   * @param {number} [index=0] The index to read.
117
   * @return {number}
118
   * @private
119
   */
120
  unpackSigned_(buffer, index=0) {
121
    return this.sign_(this.unpack_(buffer, index));
122
  }
123
124
  /**
125
   * Throws a RangeError if number is out of boundaries, return
126
   * the number otherwise.
127
   * @param {number} num The number.
128
   * @return {number}
129
   * @throws {RangeError} If number is out of boundaries.
130
   * @private
131
   */
132
  overflow_(num) {
133
    if (num > this.max || num < this.min) {
134
      throw new RangeError();
135
    }
136
    return num;
137
  }
138
139
  /**
140
   * Clamp values on overflow.
141
   * @param {number} num The number.
142
   * @private
143
   */
144
  clamp_(num) {
145
    if (num > this.max) {
146
      return this.max;
147
    } else if (num < this.min) {
148
      return this.min;
149
    }
150
    return num;
151
  }
152
153
  /**
154
   * Sign a number.
155
   * @param {number} num The number.
156
   * @return {number}
157
   * @private
158
   */
159
  sign_(num) {
160
    if (num > this.max) {
161
      num -= (this.max * 2) + 2;
162
    }
163
    return num;
164
  }
165
}
166